package cn.biplam.front.component.view.systemManage.sysprivi.privilege.deploy;

import cn.biplam.common.ajxcode.SystemCode.PriviDeployCode;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.SysManageCode.PriviDeployOptCode;
import cn.biplam.common.paramters.SysCaption;
import cn.biplam.common.paramters.UserPriviCaption;
import cn.biplam.common.struc.SysStruc.PriviDeployStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.ComponentBase;
import cn.biplam.front.component.data.common.ButtonComp;
import cn.biplam.front.component.data.common.CaptionComp;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Float_Type;
import cn.biplam.front.template.base.ButtonGroup;
import cn.biplam.front.template.buttons.AddButton;
import cn.biplam.front.template.buttons.RefreshButton;
import cn.biplam.front.template.control.FlowTable;
import cn.biplam.front.template.control.table.TablePropertys;
import cn.biplam.front.template.control.table.TablePropertys.Col_Content_Type;
import cn.biplam.front.template.pageFrame.mdManage.MdObject;
import cn.biplam.front.template.pageFrame.mdManage.MdPage;

/**
 *  权限发布管理主页面
 * @author XiaoRuxing Dec 5, 20093:22:17 PM
 * @version 1.0
 */
public class PriviDeployMain extends ComponentBase{

	/** 页面ID */
	private static String cId = PriviDeployCode.Privi_Deploy_Main;
	/** 主区域页面ID */
	private static String Main_Area_ID = cId+"t";
	/** 权限大类列表控件ID */
	public static String PriviTypeList_Cid = Main_Area_ID+"1";

	public PriviDeployMain(UserInfo user, String homeUrl, String recordId,
			KvMap<String, String> params) {
		super(user, homeUrl, recordId, params);
	}

	/**
	 * @return 权限发布管理主页面
	 * @throws ErrorMessage 
	 */
	public HtmlWindow get() throws ErrorMessage{
		MdPage mp=new MdPage(cId,sHomeUrl,mUser);
		/** 设置界面标题logo */
		mp.setTitleLogo(UserPriviCaption.PriviDeployManage_Image);
		/** 设置界面标题 */
		mp.setTitleDesc(CaptionComp.get(mUser, UserPriviCaption.PriviDeployManage_Title));

		//创建列表页面模板
		MdObject mdo = new MdObject(Main_Area_ID);
		//放入权限大类列表
		HtmlWindow priviTypeList=this.getTable(PriviTypeList_Cid);
		mdo.putTable(priviTypeList);
		//放入操作按钮
		mdo.putOperator(this.getButtonGroup());
		
		mp.putManage(mdo.get());
		return mp.get();
	}

	/**
	 * 生成操作按钮并绑定按钮事件
	 * @param mUser
	 * @return	HtmlWindow
	 * @throws ErrorMessage
	 */
	private HtmlWindow getButtonGroup() throws ErrorMessage{
		ButtonGroup operatorButtons=new ButtonGroup("",Float_Type.Left);
		if(recordId!=null){
			//刷新
			operatorButtons.put(new RefreshButton(	
					mUser,
					cId,
					PriviDeployCode.Privi_Deploy_Main,
					recordId
			).get());
			//添加
			operatorButtons.put(new AddButton(
					mUser,
					cId+"add",
					PriviDeployCode.Privi_Deploy_Add,
					recordId
			).get());
		}
		return operatorButtons.get();
	}

	/**
	 * 返回列表 
	 * @return	HtmlWindow
	 * @throws ErrorMessage
	 */
	private HtmlWindow getTable(String pageId) throws ErrorMessage{
		//创建表格模板用的属性类
		TablePropertys property = this.getProperty();
		//创建表格模板
		FlowTable table=new FlowTable(pageId,PriviDeployStruc.DEPLOY_ID,property); 
		//获取列表数据结果集
		OperatorResult rs = Operator.get(PriviDeployOptCode.Query_All, //查询所有权限大类
				mUser.getUserID(), 
				new DataFilter());

		if(TOOLS.exist(rs)){
			/** 将数据集放入表格 */
			table.put(rs);
//			table.setHeight("480");
			//附加参数
			return table.get();
		}
		return  new HtmlDiv();
	}

	/**
	 * 设置操作按钮
	 * @param property
	 * @throws ErrorMessage
	 */
	private TablePropertys getProperty() throws ErrorMessage{
		TablePropertys property = new TablePropertys(sHomeUrl, mUser);
		//发布ID
		property.putCol(PriviDeployStruc.SEQ, 
				CaptionComp.get(mUser,"15")
		);
		//权限类型名称
		property.putCol(PriviDeployStruc.NAME, 
				CaptionComp.get(mUser,UserPriviCaption.PriviType_Name)
		);
		//权限管理入口ActionID
		property.putCol(PriviDeployStruc.SHOW_ACT_ID, 
				CaptionComp.get(mUser,UserPriviCaption.PriviType_EnterID)
				);
		//权限类型说明
		property.putCol(PriviDeployStruc.DESC, 
				Col_Content_Type.DESC_TEXT,
				CaptionComp.get(mUser, SysCaption.Desc)
				);
		//操作按钮
		property.putCol("operator",
				Col_Content_Type.BUTTON, 
				CaptionComp.get(mUser,SysCaption.Operator), 
				ButtonComp.get(mUser, SysCaption.Edit),
				ButtonComp.get(mUser, SysCaption.Delete));
		/** 设置动作*/
		property.setColActionPr("operator", PriviDeployCode.Privi_Deploy_Edit);
		property.setColActionPr("operator", PriviDeployCode.Privi_Deploy_Del);
		return property;
	}
}
